package com.example.homescreen.model;

//import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

import com.example.homescreen.R;
import com.example.homescreen.dao.DBProvider;
import com.example.homescreen.entity.Food;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class ListItemActivity extends Activity {

	private DBProvider provider;
	private ListView lv;
	ArrayList<Food> food;
	FoodImageAdapter adapter;
	private EditText inputSearch;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_item);

		inputSearch = (EditText) findViewById(R.id.input_search_query);
		searchByName(new View(this));
	}

	public void searchByName(View v) {
		provider = new DBProvider(this);
		Resources res = getResources();
		String[] cols = { "fId", "fName", "fNamesearch", "fAddress",
				"fDescription", "fPrice", "fImage" };
		food = new ArrayList<Food>();
		String where = "";
		if (inputSearch.getText().toString().trim().equalsIgnoreCase("")) {
			where = null;
		} else {
			where = "fNamesearch LIKE " + "'%"
					+ inputSearch.getText().toString().trim() + "%'";
		}

		Cursor c = provider.query(null, cols, where, null, null);
		while (c.moveToNext()) {
			Food f = new Food();
			f.setfId(Integer.parseInt(c.getString(c.getColumnIndex("fId"))));
			f.setfName(c.getString(c.getColumnIndex("fName")));
			f.setfNamesearch(c.getString(c.getColumnIndex("fNamesearch")));
			f.setfAddress(c.getString(c.getColumnIndex("fAddress")));
			f.setfDescription(c.getString(c.getColumnIndex("fDescription")));
			f.setfPrice(c.getString(c.getColumnIndex("fPrice")));
			f.setfImage(c.getString(c.getColumnIndex("fImage")));

			// Adding food to list
			food.add(f);
		}
		lv = (ListView) findViewById(R.id.list_view);
		lv.setFocusable(true);
		inputSearch = (EditText) findViewById(R.id.input_search_query);
		adapter = new FoodImageAdapter(this, R.layout.list_item, food, res);
		c.close();
		lv.setAdapter(adapter);
		lv.requestFocus();
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub

				Intent intent = new Intent(ListItemActivity.this,
						DetailActivity.class);
				Food test = (Food) lv.getItemAtPosition(position);
				intent.putExtra("food", test);
				startActivity(intent);
			}
		});
	}

	@Override
	public boolean onSearchRequested() {
		// TODO Auto-generated method stub
		return super.onSearchRequested();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.list_item, menu);
		return true;
	}
}
